﻿package kliment.api.google {
	import com.adobe.serialization.json.JSONDecoder;
	import kliment.api.ITranslator;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	/**
     * Dispatched when open request.
     */
    [Event(name = 'open', type = 'flash.events.Event')]
	/**
     * Dispatched when translate complete.
     */
    [Event(name = 'complete', type = 'flash.events.Event')]
	/**
	 * ...
	 * @author	Kliment
	 * @version	0.2.6
	 * 
	 * 
	 */
	// class name: kliment.api.google.GoogleTranslator
	public class GoogleTranslator extends EventDispatcher implements ITranslator {
		static private const _URL:String = '';// = 'http://translate.google.com/translate_a/t'; // nedd proxy script
		static private const _CLIENT:String = 't';
		private var _data:Object;
		
		public var method:String = URLRequestMethod.GET;
		
		public function GoogleTranslator() {
			
		}
		
		public function translate(str:String, fromLang:String, toLang:String):void {
			var urlVars:URLVariables = new URLVariables();
			urlVars.client = _CLIENT;
			urlVars.text = str;
			urlVars.sl = fromLang;
			urlVars.tl = toLang;
			
			_load(urlVars, _URL);
		}
		
		private function _load(urlVars:URLVariables, url:String):void {			
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			urlLoader.addEventListener(Event.OPEN, _onLoaderHandler);
			urlLoader.addEventListener(Event.COMPLETE, _onLoaderHandler);
			
			var urlRequest:URLRequest = new URLRequest(url);
			urlRequest.method = method;
			urlRequest.data = urlVars;
			urlLoader.load(urlRequest);
		}
		
		private function _onLoaderHandler(event:Event):void {
			switch(event.type) {
				case Event.COMPLETE:
					_data = new JSONDecoder(event.target.data as String, true).getValue() as Object;
				break;
			}
			dispatchEvent(event);
		}
		
		/**
		 * responseData.sentences[0].trans = the-translated-text
		 * responseData.sentences[0].orig = the-original-text
		 * responseData.sentences[0].translit = the-translit-text
		 * 
		 * responseData.dict[0].pos = 
		 * responseData.dict[0].terms[0] = 
		 * responseData.dict[0].terms[1] = 
		 * responseData.dict[0].terms[..] = 
		 * responseData.dict[0].terms[m] = 
		 * responseData.dict[1].pos = 
		 * responseData.dict[1].terms[0] = 
		 * responseData.dict[1].terms[1] = 
		 * responseData.dict[1].terms[..] = 
		 * responseData.dict[1].terms[m] = 
		 * responseData.dict[..].pos = 
		 * responseData.dict[..].terms[0] = 
		 * responseData.dict[..].terms[1] = 
		 * responseData.dict[..].terms[..] = 
		 * responseData.dict[..].terms[m] = 
		 * responseData.dict[n].pos = 
		 * responseData.dict[n].terms[0] = 
		 * responseData.dict[n].terms[1] = 
		 * responseData.dict[n].terms[..] = 
		 * responseData.dict[n].terms[m] = 
		 * 
		 * responseData.src = the-detected-language
		 */
		
		public function get data():Object { return _data }
	}

}